﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Linq;
using System.Text;
using WinnerCMS.Common;

using WinnerCMS.Model;
namespace WinnerCMS.Data 
{
    public class ExchangeDal
    {
        #region 检查
        /// <summary>
        /// 检查
        /// </summary>
        /// <param name="which">金币Coin,点数Point,经验Experience</param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int Check(string which, int userId)
        { 
            using (Entities db = new Entities())
            {
                switch (which.ToLower())
                {
                    case "coin": 
                        return db.Users.Where(x => x.Id == userId).Select(x => x.Coin).First();

                    case "point": 
                        return db.Users.Where(x => x.Id == userId).Select(x => x.Point).First();
                    case "experience": 
                        return db.Users.Where(x => x.Id == userId).Select(x => x.Experience).First();
                    default:
                        throw new Exception("参数错误");

                }
            }
        }
        
        #endregion

        #region 金币转点数
        /// <summary>
        /// 金币转点数
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="coin"></param>
        /// <param name="point"></param>
        public void Coin2Point(int userId, int coin, int point)
        { 
            using(Entities db = new Entities())
            {
                var q = db.Users.First(x => x.Id == userId);
                q.Coin = q.Coin + coin;
                q.Point = q.Point - point;

                db.SaveChangesAsync().Wait();
            }
        }
        
        #endregion

        #region 点数转金币
        /// <summary>
        /// 点数转金币
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="point"></param>
        /// <param name="coin"></param>
        public void Point2Coin(int userId, int point, int coin)
        {
            using (Entities db = new Entities())
            {
                var q = db.Users.First(x => x.Id == userId);
                q.Coin = q.Coin - coin;
                q.Point = q.Point+ point;

                db.SaveChangesAsync().Wait();
            } 
        }
        
        #endregion

        #region 经验转点数
        /// <summary>
        /// 经验转点数
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="experience"></param>
        /// <param name="point"></param>
        public void Experience2Point(int userId, int experience, int point)
        {
            using (Entities db = new Entities())
            {
                var q = db.Users.Where(x => x.Id == userId).First();
                q.Coin = q.Experience - experience;
                q.Point = q.Point + point;

                db.SaveChangesAsync().Wait();
            } 
        }
        
        #endregion

        #region 金币转赠
        /// <summary>
        /// 金币转赠
        /// </summary>
        /// <param name="userName">对方用户名</param>
        /// <param name="me">我的用户名</param>
        /// <param name="coin">数量</param>
        public void CoinGive(string userName, string me, int coin)
        {  
            using (Entities db = new Entities())
            { 
                db.Database.ExecuteSqlCommand("update CMS_Users set Coin=Coin-{0} where Name={1}", coin, me);
                db.Database.ExecuteSqlCommand("update CMS_Users set Coin=Coin+{0} where Name={1}", userName);
            }
        }
        
        #endregion
    }
}